package sz.szsmk.citizencard.ui.adapter;

import android.app.Application;
import android.content.Context;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import com.alibaba.android.vlayout.DelegateAdapter;
import com.alibaba.android.vlayout.LayoutHelper;
import com.alibaba.android.vlayout.layout.DefaultLayoutHelper;
import com.bumptech.glide.Glide;

import sz.szsmk.citizencard.DaggerApplication;
import sz.szsmk.citizencard.databinding.HomeHotDotItemViewBinding;
import sz.szsmk.citizencard.databinding.HomeHotDotViewBinding;
import sz.szsmk.citizencard.model.network.bean.response.QueryPageResourceRes;
import sz.szsmk.citizencard.util.ACache;
import sz.szsmk.citizencard.util.JumpUtils;

/**
 * Copyright (c)2018
 * author: zhf
 * created on: 2018/8/40 10:47
 * description:
 */
public class HomeHotDotAdapter extends DelegateAdapter.Adapter<HomeHotDotAdapter.HomeHotDotViewHolder> {

    private static final int HOME_HOT_DOT = 0x27;

    private Context mContext;

    private QueryPageResourceRes viewData;

    private DefaultLayoutHelper mDefaultLayoutHelper;


    public interface BaseInterface {
        public void onClick(View view, String url, String syslabl, int needHeader, int resource_id,int modularId);
    }

    public void setBaseInterface(BaseInterface baseInterface) {
        this.baseInterface = baseInterface;
    }

    private BaseInterface baseInterface;


    public HomeHotDotAdapter(Application ins, DefaultLayoutHelper hotSaleHelper) {
        mContext = ins;
        mDefaultLayoutHelper = hotSaleHelper;
    }

    @Override
    public LayoutHelper onCreateLayoutHelper() {
        return mDefaultLayoutHelper;
    }

    @Override
    public HomeHotDotViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == HOME_HOT_DOT)
            return HomeHotDotViewHolder.creat(parent);
        else
            return null;
    }

    @Override
    public void onBindViewHolder(HomeHotDotViewHolder holder, int position) {
        if (viewData != null) {
            if (holder.itemView instanceof RelativeLayout) {
                RecyclerView recyclerView = (RecyclerView) holder.binding.hotList;
                LinearLayoutManager layoutManager = new LinearLayoutManager(mContext);
                layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
                recyclerView.setLayoutManager(layoutManager);
                holder.binding.title.setVisibility(View.VISIBLE);
                holder.binding.desc.setVisibility(View.VISIBLE);
                holder.binding.title.setText(viewData.getMain_title());
                holder.binding.desc.setText(viewData.getSub_title());

                recyclerView.setAdapter(new RecyclerView.Adapter<ItemViewHolder>() {

                    @Override
                    public ItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
                        return ItemViewHolder.creat(parent);
                    }

                    @Override
                    public void onBindViewHolder(ItemViewHolder holder, int position) {
                        final QueryPageResourceRes.ResourceListBean item = viewData.getResource_list().get(position);
                        if (item == null)
                            return;

                        if (position == 0)
                            holder.binding.tempView.setVisibility(View.VISIBLE);
                        else
                            holder.binding.tempView.setVisibility(View.GONE);

                        Glide.with(mContext)
                                .load(item.getResource_url())
                                .override(340, 260)
                                .into(holder.binding.cardImage);

//                        if (TextUtils.isEmpty(item.getLink_url())) {
                        holder.binding.cardImage.setOnClickListener(new View.OnClickListener() {

                            private String mUrl;

                            @Override
                            public void onClick(View view) {
                                String access_token = ACache.get(DaggerApplication.getIns()).getAsString("access_token");
//                                String url = item.getLink_url();
                                if (access_token == null) {
                                    JumpUtils.jump(mContext, "login", 0);
                                } else {
                                    QueryPageResourceRes.ResourceListBean bean = viewData.getResource_list().get(position);
                                    int need_header = bean.getNeed_header();
                                    String url = bean.getLink_url();
                                    String sys_label = bean.getSys_label();
                                    int resource_id = bean.getResource_id();
                                    int modularId = bean.getModularId();
                                    if (access_token == null) {
                                        JumpUtils.jump(mContext, "login", 0);
                                        return;
                                    }
                                    if (baseInterface != null) {
                                        baseInterface.onClick(null, url, sys_label, need_header, resource_id,modularId);
                                    }
                                }
                            }
                        });
                    }

                    @Override
                    public int getItemCount() {
                        return (viewData != null && viewData.getResource_list() != null &&
                                viewData.getResource_list().size() != 0) ? viewData.getResource_list().size() : 0;
                    }
                });
            }

        }

    }

    @Override
    public int getItemCount() {
        return 1;
    }

    @Override
    public int getItemViewType(int position) {
        return HOME_HOT_DOT;
    }

    public void setViewData(QueryPageResourceRes viewData) {
        if (viewData != null) {
            this.viewData = viewData;
            notifyDataSetChanged();
        }
    }

    /**
     * 横向滚动Holder 容器
     */
    static class HomeHotDotViewHolder extends RecyclerView.ViewHolder {
        public HomeHotDotViewBinding binding;

        public static HomeHotDotViewHolder creat(ViewGroup parent) {
            HomeHotDotViewBinding binding = HomeHotDotViewBinding
                    .inflate(LayoutInflater.from((parent.getContext())), parent, false);
            return new HomeHotDotViewHolder(binding);
        }

        public HomeHotDotViewHolder(HomeHotDotViewBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }
    }

    /**
     * item项的列表 子项
     */
    static class ItemViewHolder extends RecyclerView.ViewHolder {

        public HomeHotDotItemViewBinding binding;

        public static ItemViewHolder creat(ViewGroup parent) {
            HomeHotDotItemViewBinding binding = HomeHotDotItemViewBinding
                    .inflate(LayoutInflater.from((parent.getContext())), parent, false);
            return new ItemViewHolder(binding);
        }

        public ItemViewHolder(HomeHotDotItemViewBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }
    }
}
